package com.qxy.hgz;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;
import com.google.android.material.tabs.TabLayout;
import com.qxy.hgz.databinding.ActivityHomeBinding;
import com.qxy.hgz.rank.ui.RankListActivity;
import com.qxy.hgz.user.view.BlankFragment;
import com.qxy.hgz.user.view.UserHomeFragment;


public class HomeActivity extends AppCompatActivity {
    private DouYinOpenApi douYinOpenApi;
    private ActivityHomeBinding binding;
    private FragmentManager manager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityHomeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        douYinOpenApi = DouYinOpenApiFactory.create(this);

        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        transaction.replace(R.id.main_container_fragment, new BlankFragment());
        transaction.commit();

        binding.tabSet.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                FragmentTransaction transaction = manager.beginTransaction();
                if (tab.getPosition() == 4) {
                    transaction.replace(R.id.main_container_fragment, new UserHomeFragment());
                } else {
                    transaction.replace(R.id.main_container_fragment, new BlankFragment());
                }
                transaction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.list_button) {
            startActivity(new Intent(HomeActivity.this, RankListActivity.class));
        } else if (item.getItemId() == R.id.auth_button) {
            auth();
        }
        return true;
    }

    private void auth() {
        Authorization.Request request = new Authorization.Request();
        request.scope = "user_info";// 用户授权时必选权限
        request.state = "ww"; // 用于保持请求和回调的状态，授权请求后原样带回给第三方。
        //request.callerLocalEntry = "com.xxx.xxx...activity";
        douYinOpenApi.authorize(request);
        // 优先使用抖音app进行授权，如果抖音app因版本或者其他原因无法授权，则使用web页授权
    }
}